import telnetlib

def send_telnet_command(host, port, command):
    try:
        # 连接telnet服务器
        tn = telnetlib.Telnet(host, port, timeout=10)
        print('连接成功')
        
        # 发送命令，以\r\n为发送回车
        tn.write(command.encode('ascii') + b'\r\n')
        
        # 读取返回值, read_until读取方法，遇到\r\n\r\n停止
        response = tn.read_until(b'\r\n\r\n').decode('ascii')
        
        # 关闭连接
        tn.close()

        return response
    except Exception as e:
        return str(e)

# 设置telnet服务器的主机和端口
HOST = "169.254.109.138"
PORT = 3602

# 发送的命令
command = 'RDSMPSDATA3'

# 发送命令并获取返回值
response = send_telnet_command(HOST, PORT, command)

# 将返回值写入文件
with open('1.txt', 'w') as file:
    file.write(response)

print("返回值:", response)